<html>
<head>
	<meta charset="UTF-8">
	<title>内部模型查询条件新增</title>
	<link id="easyuiTheme"  href="../../static/easyui/themes/bootstrap/easyui.css" rel="stylesheet" type="text/css"/>
	<link href="../../static/easyui/themes/icon.css" rel="stylesheet" type="text/css"/>
	<link href="../../static/css/esaas-easyui-add.css" rel="stylesheet" type="text/css"/>
	<script src="../../static/easyui/jquery.min.js" type="text/javascript"></script>
	<script src="../../static/easyui/jquery.cookie.js" type="text/javascript"></script>
	<script src="../../static/easyui/ui-theme.js" type="text/javascript"></script>
	<script src="../../static/easyui/jquery.easyui.min.js" type="text/javascript" charset="utf-8"></script>
	<script src="../../static/easyui/locale/easyui-lang-zh_CN.js" type="text/javascript"></script>
	<script src="../../static/plugin/assets/jslib.util.js" type="text/javascript"></script>
	<script src="../../static/plugin/assets/jslib.jquery.ajax.js" type="text/javascript"></script>
	<script src="../../static/plugin/assets/jslib.easyui.util.js" type="text/javascript"></script>
	<script src="../../ffres/dict/all.js" type="text/javascript"></script>
	<a href="" target="_blank" id="commonLink" name="commonLink"></a>
	<style>
		body {
			overflow: auto !important
		}
		#ff table td {
			vertical-align:top
		}
	</style>
</head>
<body class="easyui-layout">
<form id="ff" method="post">
	<div class="esaas-info-title">内部模型查询条件信息</div>
	<table class="esaas-info-add-table">
		<tr hidden="true">
			<td width="15%" align="right">条件标识：</td>
			<td width="35%"><input class="easyui-textbox" type="text"
								   name="condId" data-options="required:false"
								   style="width: 100%; height: 25px;"></input></td>
			<td width="15%" align="right">应用模型标识：</td>
			<td width="35%"><input class="easyui-textbox" type="text"
								   name="appModelId" data-options="required:false" id="appModelId"
								   style="width: 100%; height: 25px;"></input></td>
		</tr>
		<tr hidden="true">
			<td width="15%" align="right">条件字段代码：</td>
			<td width="35%"><input class="easyui-textbox" type="text"
								   name="condFieldCode" data-options="required:false"
								   style="width: 100%; height: 25px;"></input></td>
			<td width="15%" align="right">条件字段名称：</td>
			<td width="35%"><input class="easyui-textbox" type="text"
								   name="condFieldName" data-options="required:false"
								   style="width: 100%; height: 25px;"></input></td>
		</tr>
		<tr>
			<td width="15%" align="left">条件逻辑关系：</td>
			<td width="20%"><select class="easyui-combobox"
									id="condLogicRelation" name="condLogicRelation"
									style="width: 100%; height: 25px;"
									data-options="required:false,editable:false,valueField:'enname',textField:'cnname',data:COND_LOGIC_RELATION">
			</select></td>
			<td></td>
			<td></td>
		</tr>
		<tr>
			<td width="15%" align="left">条件字段：</td>
			<td width="15%" align="left">比较符：</td>
			<td width="15%" align="left">比较目标类型：</td>
			<td width="15%" align="left" class="paramField">输入参数：</td>
			<td width="15%" align="left" class="constValue">常量值：</td>
		</tr>
		<tr>
			<td width="35%">
				<ul class="easyui-datalist" id="condFieldList" data-options="valueField:'paramFieldCode', textField:'text'"></ul>
			</td>
			<td width="20%"><select class="easyui-combobox"
									id="condCompareSign" name="condCompareSign"
									style="width: 100%; height: 25px;"
									data-options="required:false,editable:false,valueField:'enname',textField:'cnname',data:COND_COMPARE_SIGN">
			</select></td>
			<td width="20%"><select class="easyui-combobox"
									id="compareTargetType" name="compareTargetType"
									style="width: 100%; height: 25px;"
									data-options="required:false,editable:false,valueField:'enname',textField:'cnname',data:COMPARE_TARGET_TYPE, onChange:compareTargetChange">
			</select></td>
			<td width="35%" class="paramField">
				<ul class="easyui-datalist" id="paramFieldList" data-options="valueField:'paramFieldCode', textField:'text'"></ul>
			</td>
			<td width="35%" class="constValue"><input class="easyui-textbox" type="text"
													  name="constValue" data-options="required:false"
													  style="width: 100%; height: 25px;"></input></td>
		</tr>
		<tr hidden="true">
			<td width="15%" align="right">参数名称：</td>
			<td width="35%"><input class="easyui-textbox" type="text"
								   name="paramFieldName" data-options="required:false"
								   style="width: 100%; height: 25px;"></input></td>
			<td width="15%" align="right">参数代码：</td>
			<td width="35%"><input class="easyui-textbox" type="text"
								   name="paramFieldCode" data-options="required:false"
								   style="width: 100%; height: 25px;"></input></td>
		</tr>
	</table>
</form>
<div style="text-align: center; padding: 15px">
	<a href="javascript:void(0)" class="easyui-linkbutton"
	   onclick="submitForm()">&nbsp;保&nbsp;存&nbsp;</a> <a
		href="javascript:void(0)" class="easyui-linkbutton"
		onclick="closeForm()">&nbsp;关&nbsp;闭&nbsp;</a>
</div>
<script>

	var appId = $.getUrlParam('appId');
	var appModelId = $.getUrlParam('appModelId');
	$(function() {
		$('#appModelId').textbox('setValue', appModelId);
		loadTableFields(appModelId)
		loadIsrvInoutInfos(appModelId)
		compareTargetChange('2') // hide constValue for init
	});

	function submitForm() {
		if (!$('#ff').form('validate'))
			return;
		var jsonData = $("#ff").serializeJson();

		var condFieldSelect = $('#condFieldList').datagrid('getSelected');
		if (!condFieldSelect) {
			$.messager.alert('提示','请选择一条条件字段！','info');
			return;
		}

		jsonData.condFieldCode = condFieldSelect.paramFieldCode;
		jsonData.condFieldName = condFieldSelect.paramFieldName;

		if (jsonData.compareTargetType == "2") {
			var paramFieldSelect = $('#paramFieldList').datagrid('getSelected');
			if (!paramFieldSelect) {
				$.messager.alert('提示','请选择一条输入参数！','info');
				return;
			}

			jsonData.paramFieldCode = paramFieldSelect.paramFieldCode;
			jsonData.paramFieldName = paramFieldSelect.paramFieldName;
			jsonData.constValue = "";
		}


		$.restfulPostJson('../../api/isrv/cond/info', jsonData, function(data) {
			window.parent.reloadList();
		});
	};

	function loadTableFields(appModelId) {
		var objectId;
		$.restfulGet('../../api/app/model/info/' + appModelId,null,function(data){
			objectId = data.rows.tableModelId;
			var url = `../../api/model/table/fields`;
			$.restfulPostForm(url, {
				objectId: objectId ,
				size: 100
			}, function(data) {
				var rows = data.rows;
				if (rows && rows.length > 0 ) {
					rows.forEach( f => {
						f.paramFieldCode = f.fieldCode;
						f.paramFieldName = f.fieldName;
						f.text = `${f.fieldCode} ${f.fieldName ? f.fieldName : ''}`;
					})
					$('#condFieldList').datalist({data: rows})
				}

			});
		});

	}

	function loadIsrvInoutInfos(appModelId) {
		$.restfulPostForm(`../../api/isrv/inout/infos`, {
			ioType: "I",
			appModelId : appModelId
		}, function(data){
			data.rows.forEach(f =>{
				f.text = `${f.paramFieldCode} ${f.paramFieldName ? f.paramFieldName: "" }`
			})
			$('#paramFieldList').datalist({data: data.rows})
		})
	}

	function compareTargetChange(newValue, oldValue) {
		if (newValue == "2") {
			// 常量
			$('.constValue').hide();
			$('.paramField').show();
		} else {
			$('.constValue').show();
			$('.paramField').hide();
		}
	}

	function closeForm() {
		window.parent.JLEUtil.closewin();
	}
</script>
</body>
</html>
